package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
//import com.heima.user.entity.ApAuthor;
import com.heima.user.dto.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author gq
 * @since 2022-06-21
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    private IApUserService userService;

    @Override
    public ResponseResult listByStatus(AuthDto authDto) {
//        用户认证列表分页查询
//        判断传入状态是否为空，为空则查询全部，不为空则添加查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (authDto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        }
        IPage<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, queryWrapper);
        PageResponseResult ret = new PageResponseResult(authDto.getPage(), authDto.getSize(), iPage.getTotal(), iPage.getRecords());
        return ret;
    }

    /**
     * 审核
     *
     * @param dto
     * @param type 1：成功   0 ： 失败
     * @return
     */
    @Override
    public ResponseResult auth(AuthDto dto, int type) {
//        根据ID查询用户认证表的数据
        ApUserRealname userRealname = this.getById(dto.getId());
//        失败，修改认证状态为2 ，并保存失败的原因
        if (type == 0) {
            userRealname.setStatus(2);
            userRealname.setReason(dto.getReason());
            this.updateById(userRealname);
        }

//        审核成功流程
//              查询ap_user
        ApUser apUser = userService.getById(userRealname.getUserId());
//               创建自媒体账号，注册信息从ap_user同步
        WmUser wmUser = createWmUser(apUser);
//        创建作者账号
        if(wmUser != null){
            ApAuthor author = createAuthor(apUser, wmUser);
            if (apUser != null){
//                认证表的状态为9 表示审核通过
                userRealname.setStatus(9);
                this.updateById(userRealname);
//                修改用户注册表的flag为1 自媒体人
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    @Autowired
    private MediaClient mediaClient;
    @Autowired
    private ArticleClient articleClient;
    /**
     * 创建自媒体用户
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
//        构建自媒体用户
        WmUser wmUser = new WmUser();
//        基本属性从apUser复制
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
//        远程调动自媒体接口
        ResponseResult<WmUser> responseResult = mediaClient.saveWmUser(wmUser);
//        判断调用结果
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser user = responseResult.getData();
            return user;
        }
        return null;
    }
    /**
     * 创建文章作者
     *
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createAuthor(ApUser apUser, WmUser wmUser) {
        // 构建文章作者
       /* ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());*/
        // 远程调用文章服务接口
//        ResponseResult<ApAuthor> responseResult = articleClient.saveApAuthor(author);
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> apAuthorResponseResult = articleClient.saveApAuthor(author);
        // 判断操作成功
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            // 返回作者数据
//            ApAuthor apAuthor = apAuthorResponseResult.getData();
           ApAuthor resultData = apAuthorResponseResult.getData();
            // 作者的id写回到自媒体用户表中
            wmUser.setApAuthorId(resultData.getId());
            mediaClient.updateWmUser(wmUser);
            return resultData;
        }
        return null;
    }

}
